package com.lcm.weam.service.sys.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lcm.weam.dao.sys.UserMapper;
import com.lcm.weam.entity.resp.PageResult;
import com.lcm.weam.entity.sys.FileList;
import com.lcm.weam.entity.sys.User;
import com.lcm.weam.service.sys.UserService;
import com.lcm.weam.util.AliOSSUtil;
import com.lcm.weam.util.JavaEmailUtil;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.mail.MessagingException;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    private static final int HASHITERATIONS = 3;

    @Value("${server.host}")
    private String host;
    @Value("${server.port}")
    private String port;

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> selectAll() {
        return userMapper.selectAll();
    }

    @Override
    public User selectById(String uid) {
        return userMapper.selectById(uid);
    }

    @Override
    public User selectByMobile(String mobile) {
        return userMapper.selectByMobile(mobile);
    }

    @Override
    @Transactional
    public void updateUser(User user) {
        userMapper.updateUser(user);
    }

    @Override
    public Boolean checkPassword(String oldPassword, String uid) {
        User user = selectById(uid);
        String password = user.getPassword();
        String old = new Md5Hash(oldPassword, user.getMobile(), HASHITERATIONS).toString();
        return password.equals(old);
    }

    @Override
    @Transactional
    public void changePassword(String newPassword, String uid) {
        userMapper.changePassword(new Md5Hash(newPassword, selectById(uid).getMobile(), HASHITERATIONS).toString(),uid);
    }


    //无条件分页查询
    @Override
    public IPage<User> selectPageUser(int pageIndex, int pageSize) {
        return userMapper.selectAll(new Page<User>(pageIndex, pageSize));
    }

    //上传头像到aliyunOSS【不加密】
    @Override
    @Transactional
    public String uploadPhotoAliOSS(String uid, MultipartFile file) throws IOException {
        AliOSSUtil ossUtils = new AliOSSUtil();
        String userPhoto = ossUtils.upload(file, "userPhoto", uid, false);
        userMapper.updateUserPhoto(userPhoto, uid);
        return userPhoto;
    }

    @Override
    @Transactional
    public void register(User user) {
        userMapper.register(user);
    }

    //分页列举文件
    @Override
    public PageResult<FileList> listFiles(String uid, int page, int num) {
        AliOSSUtil ossUtils = new AliOSSUtil();
        Map<String, Object> map = ossUtils.listFiles(uid, page, num);
        PageResult<FileList> pageResult = new PageResult<>();
        pageResult.setNums((Integer) map.get("nums"));
        pageResult.setTotal((Integer) map.get("total"));
        pageResult.setRows((List<FileList>) map.get("rows"));
        return pageResult;
    }

    @Override
    public void uploadFileAliOSS(String uid, MultipartFile file, String fileName) throws IOException {
        AliOSSUtil ossUtils = new AliOSSUtil();
        String url = ossUtils.upload(file, uid, fileName, true);
        System.out.println(url);
    }

    @Override
    public void send(String username, String email, String uid) throws MessagingException {
        //邮件内容
        //准备文本
        StringBuilder sb = new StringBuilder();
        sb.append("<p>");
            sb.append("<span  style='font-family:arial;color:black;font-size:20px'>欢迎您，</span>");
            sb.append("<span style='font-family:arial;color:red;font-size:20px'>");
            sb.append(username);
            sb.append("</span>");
        sb.append("</p>");
        sb.append("<p style='text-indent:2em'>");
            sb.append("<a style='font-family:arial;color:black;font-size:20px' href='");
            sb.append("http://localhost:613/sys/user/").append(uid).append("/auth");
            sb.append("'>点击此链接完成您的邮箱验证</a>");
        sb.append("</p>");
        //字符串加入邮件
        MimeBodyPart text = new MimeBodyPart();
        text.setContent(sb.toString(), "text/html;charset=utf-8");
        //拼装邮件正文
        MimeMultipart mimeMultipart = new MimeMultipart();
        mimeMultipart.addBodyPart(text);
        mimeMultipart.setSubType("related");
        //将拼装好的正文内容设置为主体
        MimeBodyPart contentText = new MimeBodyPart();
        contentText.setContent(mimeMultipart);
        //拼接附件
        MimeMultipart allFile = new MimeMultipart();
        allFile.addBodyPart(contentText);//正文
        allFile.setSubType("mixed"); //正文和附件都存在邮件中，所有类型设置为mixed
        JavaEmailUtil.sandMail("欢迎使用WEAM",email,allFile);
    }

    @Override
    public void auth(String uid) {
        //数据库中更新
        userMapper.auth(uid);
    }


}
